package com.woniu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.Result;
import com.woniu.entity.vo.SystemMessageVO;
import com.woniu.entity.vo.UserLoginVO;
import com.woniu.pojo.Message;
import com.woniu.pojo.User;
import com.woniu.service.ICommentService;
import com.woniu.service.ILikeService;
import com.woniu.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title
 * @Author 武术有栖
 * @Description:
 */
@Controller
@Api(tags = "消息模块")
@RequestMapping("/message")
@Slf4j
public class MessageController extends BaseController{
    @Autowired
    private IMessageService messageService;
    @Autowired
    private ICommentService commentService;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private RedisTemplate redisTemplate;

/*    @GetMapping("/like/{id}")//用户id
    @ApiOperation("点赞消息展示thymeleaf")
    public String listLike(@PathVariable String id, Model model){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",0);
        List<Message> messageList = messageService.list(wrapper);
        model.addAttribute(messageList);
        System.out.println(messageList);
        return "message";
    }
    @GetMapping("/comment/{id}")//用户id
    @ApiOperation("评论消息展示thymeleaf")
    public String listComment(@PathVariable String id, Model model){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",1);
        List<Message> messageList = messageService.list(wrapper);
        model.addAttribute(messageList);
        System.out.println(messageList);
        return "message";
    }
    @GetMapping("/system/{id}")
    @ApiOperation("系统消息展示thymeleaf")
    public String listSystem(Model model, @PathVariable String id){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("type",2);
        List<Message> messageList = messageService.list(wrapper);
        List<SystemMessageVO> messageVOList = new ArrayList<>();
        for (Message i:messageList) {
            SystemMessageVO systemMessageVO = new SystemMessageVO();
            systemMessageVO.setMessage(i);
            Boolean flag = redisTemplate.opsForSet().isMember("readable_message_"+i.getId(),Integer.parseInt(id));
            systemMessageVO.setIsRead(flag);
            messageVOList.add(systemMessageVO);
            System.out.println(systemMessageVO);
        }
        model.addAttribute(messageVOList);
        System.out.println(messageVOList);
        return "systemMessage";
    }
    @GetMapping("/system/read/{id}/{messageId}")
    @ApiOperation("更新系统消息为已读thymeleaf")
    public String readSystem(Model model, @PathVariable String id, @PathVariable String messageId){
        redisTemplate.opsForSet().remove("readable_message_"+messageId,Integer.parseInt(id));
        return "systemMessage";
    }
    @GetMapping("/read/{id}/{messageId}")
    @ApiOperation("更新消息（非系统消息）为已读thymeleaf")
    public String readMessage(Model model, @PathVariable String id, @PathVariable String messageId){
        Message message = messageService.getById(messageId);
        message.setStatus((byte) 1);
        messageService.updateMessage(message);
        return "message";
    }*/
    @GetMapping("/like/{id}")//用户id
    @ApiOperation("点赞消息展示")
    @ResponseBody
    public Result listLikeMessage(@PathVariable String id){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",0);
        List<Message> messageList = messageService.list(wrapper);
        return toDataResult(messageList);
    }
    @GetMapping("/comment/{id}")//用户id
    @ApiOperation("评论消息展示")
    @ResponseBody
    public Result listCommentMessage(@PathVariable String id){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        wrapper.eq("type",1);
        List<Message> messageList = messageService.list(wrapper);
        return toDataResult(messageList);
    }
    @GetMapping("/system/{id}")
    @ApiOperation("系统消息展示")
    @ResponseBody
    public Result listSystemMessage(@PathVariable String id){
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("type",2);
        //检索出所有的系统消息
        List<Message> messageList = messageService.list(wrapper);
        //创建一个系统消息VO对象用来保存系统消息和系统消息对于特定用户的未读/已读状态
        List<SystemMessageVO> messageVOList = new ArrayList<>();
        for (Message i:messageList) {
            //遍历系统消息对象
            SystemMessageVO systemMessageVO = new SystemMessageVO();
            systemMessageVO.setMessage(i);
            //检索相应的系统消息在redis中的set集合中是否存在这个用户的id 存在则为未读 不存在则为已读
            Boolean flag = redisTemplate.opsForSet().isMember("readable_message_"+i.getId(),Integer.parseInt(id));
            systemMessageVO.setIsRead(flag);
            messageVOList.add(systemMessageVO);
            System.out.println(systemMessageVO);
        }
        return toDataResult(messageVOList);
    }
    @GetMapping("/unreadLike")
    @ApiOperation("获取未读点赞消息数量")
    @ResponseBody
    public Result getUnreadLike(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        Integer unreadLike = likeService.getUnread(id);
        return toDataResult(unreadLike);
    }
    @GetMapping("/unreadComment")
    @ApiOperation("获取未读评论消息数量")
    @ResponseBody
    public Result getUnreadComment(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        Integer unreadComment = commentService.getUnread(id);
        return toDataResult(unreadComment);
    }
    @GetMapping("/unreadSystem")
    @ApiOperation("获取未读系统消息数量")
    @ResponseBody
    public Result getUnreadSystem(HttpSession session){
        User user = (User) session.getAttribute("user");
        Integer id = user.getId();
        Integer unreadComment = messageService.getUnread(id);
        return toDataResult(unreadComment);
    }
    @GetMapping("/system/read/{id}/{messageId}")
    @ApiOperation("更新系统消息为已读")
    @ResponseBody
    public Result readSystem(@PathVariable String id, @PathVariable String messageId){
        redisTemplate.opsForSet().remove("readable_message_"+messageId,Integer.parseInt(id));
        //在系统消息的set集合中删除该用户的id
        return success();
    }
    @GetMapping("/read/{id}/{messageId}")
    @ApiOperation("更新消息（非系统消息）为已读")
    @ResponseBody
    public Result readMessage(@PathVariable String id, @PathVariable String messageId){
        Message message = messageService.getById(messageId);
        message.setStatus((byte) 1);
        messageService.updateMessage(message);
        return success();
    }
}
